<template>
  <div class="jj">
    <h2 style="margin-top: 150px;text-align: center">灵动创意考试系统 - 管理后台</h2>
    <el-card class="box-card">
      <div style="padding: 0 40px">
        <h3>登 录</h3>
        <el-form
            :model="loginFrom"
            :size="'large'"
            ref="ruleFormRef"
            :rules="loginFromRules"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginFrom.username"
                placeholder="请输入用户名"
                prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginFrom.password"
                type="password"
                :show-password="true"
                placeholder="请输入密码"
                prefix-icon="Lock"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
            size="large"
            type="primary"
            style="width: 100%;margin-bottom: 30px"
            @click="login"
        >立即登录</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginFrom: {
        username: "",
        password: ""
      },
      loginFromRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs["ruleFormRef"].validate((valid) => {
        if (valid) {
          this.$request.post("/user/login", {
            userAccount: this.loginFrom.username,
            userPassword: this.loginFrom.password
          }).then((res) => {
            if (res.code === 0) {
              this.$message({
                message: "登录成功",
                type: "success"
              });

              this.$nextTick(() => {
                localStorage.setItem("token", JSON.stringify(res.data));
                this.$router.push({
                  path: "/home"
                });
              })
            }
          })
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style>
   .jj {
     height: calc(100vh - 158px);
     background: url("../../assets/bg.png") no-repeat;
      /*背景自适应*/
     background-size: cover;
  }
  .box-card{
    width: 500px;
    margin: 50px auto 0;
  }
</style>
